package com.lcxw.hxzbapi.controller.roomcontroller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.RoomRank;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.pojo.YwpdRoom;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.RedisUtils;
import com.lcxw.hxzbcommon.common.RoomUtils;

@RestController
@RequestMapping("/api/room/range")
@CrossOrigin
@Validated
public class RoomRangeController {

    private Logger logger = LoggerFactory.getLogger(RoomRangeController.class);

    @Resource
    YwpdMemberService ywpdMemberService;

    @Resource
    RoomRealization roomRealization;

    @Resource
    YwpdRoomService ywpdRoomService;

    @Resource
    RedisUtils redisUtils;

//    @PostMapping("/get/hot")
//    public Response getHotRoomRange(@RequestBody JSONObject requestParams){
//
//        String redisKey = "roomRange:"+requestParams.getString("roomType");
//
//        return getRoomRange(redisKey);
//    }

    @PostMapping("get/general")
    public Response getRoomRange(@RequestBody JSONObject requestParams) {
        String rangeType = requestParams.getString("rangeType");

        if (!"day".equals(rangeType) && !"hour".equals(rangeType) && !"week".equals(rangeType)
            && !"month"
            .equals(rangeType)) {
            return Response.wrongParam(logger, "rangeType");
        }

        String redisKey = "roomEarn:Range:" + rangeType;

        return getRoomRange(redisKey);
    }

    private Response getRoomRange(String redisKey) {
        List list = redisUtils.list_range(redisKey);

        List<RoomRank> roomRankBeans = JSON.parseArray(list.toString(), RoomRank.class);

        Map<String, Object> data = new LinkedHashMap<>();

        data.put("roomRange", roomRankBeans);

        return Response.common(logger, "get room range success", 0, "success", data);
    }

    /**
     * 获取房间广场
     */
    @PostMapping("get/roomPark")
    public Response getRoomPark(@RequestBody JSONObject requestParams) {

        String roomType = requestParams.getString("roomType");

        if (!"voice".equals(roomType) && !"video".equals(roomType) && !"fullVideo".equals(roomType) && !"paipai".equals(roomType)){
            return Response.wrongParam(logger,"roomType");
        }
        Map<String,Object> data = new LinkedHashMap<>();

        List roomRange = roomRealization.getRoomPark(roomType);

        data.put("roomRange",roomRange);

        return Response.ok(data);
    }


}
